﻿// ***********************************************************************
// Assembly         : INNOVACALL.Framework.Common
// Author           : LJO+01 (Laurent Jordi)
// Created          : 06-05-2013
// Last Modified By : LJO+01 (Laurent Jordi)
// Last Modified On : 06-05-2013
// ***********************************************************************
// <copyright file="SmtpClientSmtpAppender.cs" company="Copyright © Innovacall 2011">
//     Copyright (c) Copyright © Innovacall 2011. All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Configuration;
using System.Net.Mail;
using System.Text;
using System.Web;
using System.Web.Configuration;
using log4net.Appender;
using log4net.Core;

namespace INNOVACALL.Framework.Common.Logging
{
    /// <summary>
    /// Class SmtpClientLog4NetSmtpAppender
    /// </summary>
    public class SmtpClientLog4NetSmtpAppender : SmtpAppender
    {
        /// <summary>
        /// Sends the contents of the cyclic buffer as an e-mail message.
        /// </summary>
        /// <param name="events">The logging events to send.</param>
        override protected void SendBuffer(LoggingEvent[] events)
        {
            try
            {
                StringWriter writer = new StringWriter(
                 System.Globalization.CultureInfo.InvariantCulture);
                string t = Layout.Header;
                if (t != null)
                {
                    writer.Write(t);
                }
                for (int i = 0; i < events.Length; i++)
                {
                    // Render the event and append the text to the buffer
                    RenderLoggingEvent(writer, events[i]);
                }
                t = Layout.Footer;
                if (t != null)
                {
                    writer.Write(t);
                }
                //Add by Innovacall
                Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
                MailSettingsSectionGroup settings = (MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");
                SmtpClient smtpClient = new SmtpClient();
                smtpClient.UseDefaultCredentials = settings.Smtp.Network.DefaultCredentials;
                smtpClient.Host = settings.Smtp.Network.Host;
                smtpClient.Port = settings.Smtp.Network.Port;
                smtpClient.EnableSsl = settings.Smtp.Network.EnableSsl;
                smtpClient.Credentials = new NetworkCredential(settings.Smtp.Network.UserName, settings.Smtp.Network.Password);
                //-----------------
                MailMessage mail = new MailMessage
                (
                    From,
                    To,
                    string.Format(Subject, HttpContext.Current.Request.Url.Authority),
                    writer.ToString()
                );
                smtpClient.Send(mail);
            }
            catch (Exception e)
            {
                ErrorHandler.Error("Error occurred while sending e-mail notification.", e);
            }
        }
    }
}
